package com.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.mapper.EmpiMergeLogMapper;
import com.base.vo.EmpiMegerLog;
import com.base.vo.EmpiMergeInfo;
import com.base.service.EmpiMergeInfoService;
import com.base.mapper.EmpiMergeInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 *
 */
@Service
public class EmpiMergeInfoServiceImpl extends ServiceImpl<EmpiMergeInfoMapper, EmpiMergeInfo>
        implements EmpiMergeInfoService {

    @Autowired
    EmpiMergeLogMapper empiMergeLogMapper;

    /**
     * 执行生成EMPI的存储过程
     *
     * @param batchId
     */
    @Override
    public void callable(String batchId) {
        Assert.notNull(batchId, "调用存储过程时，传入的批次号不能为空！");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1);
        List infos = this.baseMapper.selectList(queryWrapper);
        if (!infos.isEmpty()) {
            throw new RuntimeException("当前有正在执行的任务，请稍后重试！");
        }
        this.baseMapper.propAutoMerge(batchId);
    }

    /**
     * 未合并数量
     *
     * @return
     */
    @Override
    public Integer getUnMergeCount() {
        return this.baseMapper.selectUnMergeCount();
    }

    /**
     * 获取执行明细
     *
     * @param batchId
     * @return
     */
    @Override
    public List<EmpiMegerLog> getEmpiMergeLogByBatchId(String batchId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("batch_id",batchId);
        return empiMergeLogMapper.selectList(queryWrapper);
    }
}




